package com.wswy.wzcx.ui.main.news

import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentPagerAdapter
import android.support.v4.view.ViewPager
import android.view.View
import com.wswy.wzcx.AppContext
import com.wswy.wzcx.R
import com.wswy.wzcx.model.core.EventTabNewsCategory
import com.wswy.wzcx.model.news.NewsCategory
import com.wswy.wzcx.module.base.CBaseFragment
import com.wswy.wzcx.statistics.StatTools
import com.wswy.wzcx.statistics.StatisticsId
import com.wswy.wzcx.ui.main.home.HomeNewsFragment
import com.wswy.wzcx.utils.UiUtils
import kotlinx.android.synthetic.main.fragment_main_news.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class MainNewsFragment : CBaseFragment() {

    override fun getContentLayoutId(): Int {
        return R.layout.fragment_main_news
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        UiUtils.injectStatusBar(root_content)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this)
        }


//        val defData = arrayListOf(NewsCategory("toutiao","推荐"),
//                NewsCategory("qiche","汽车"),
//                NewsCategory("yule","娱乐"),
//                NewsCategory("shehui","社会"),
//                NewsCategory("tiyu","体育"),
//                NewsCategory("caijing","财经"),
//                NewsCategory("keji","科技"),
//                NewsCategory("junshi","军事"),
//                NewsCategory("guoji","国际"),
//                NewsCategory("xiaohua","笑话"))
//
//
//        showNewsCategory(defData)

    }

    override fun onDestroy() {
        super.onDestroy()
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this)
        }

    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    fun onEventNewsCategory(event: EventTabNewsCategory) {
        val news = event.news
        if (news != null) {
            showNewsCategory(news)
        }
    }

    private fun showNewsCategory(data: List<NewsCategory>) {
        val arrays = arrayOfNulls<String>(data.size)
        data.forEachIndexed { index, newsCategory ->
            arrays[index] = newsCategory.name
        }
        for (array in arrays) {
            tabLayout.addTab(tabLayout.newTab().setText(array))
        }
        tabLayout.setupWithViewPager(vp_news)
        vp_news.offscreenPageLimit = 5
        vp_news.adapter = object : FragmentPagerAdapter(childFragmentManager) {

            override fun getCount(): Int {
                return arrays.size
            }

            override fun getItem(position: Int): Fragment {
                return HomeNewsFragment.newInstance(data[position], StatisticsId.click_newstab_news)
            }

            override fun getPageTitle(position: Int): CharSequence? {
                return arrays[position]
            }

        }

        vp_news.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

            }

            override fun onPageSelected(position: Int) {

                StatTools.sendShow(AppContext.getContext(), StatisticsId.click_News_Type, data[position].type)
            }

            override fun onPageScrollStateChanged(state: Int) {

            }
        })

        indicator.setTabLayoutAndViewPager(tabLayout, vp_news)
        tabLayout.requestLayout()
    }
}